<template>
  <el-dialog title="分配角色" :visible="showRoleDialog">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="roleIds">
      <!-- 选项 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.title }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRolesList } from '@/api/roles'
import { getStaffById, editStaff } from '@/api/staff'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      list: [],
      roleIds: []
    }
  },
  created() {
    this.backFill()
  },
  methods: {
    // 提交
    async submit() {
      // 调用了员工编辑的方法
      await editStaff({ roles: this.roleIds, id: this.userId })
      this.$message.success('分配角色成功')
      this.close()
    },
    // 数据回显
    async backFill() {
      const { data } = await getRolesList()
      this.list = data
    },
    // 根据ID获取当前角色详情
    async getRoleById(id) {
      // 调用了获取指定员工员工的方法
      const { data } = await getStaffById(id)
      const arr = []
      data.roles.forEach((item) => {
        arr.push(item.id)
      })
      this.roleIds = arr || []
    },
    // 关闭
    close() {
      this.roleIds = []
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>
</style>
